package com.taofuxn.admin.api.controller.sys;

import com.taofuxn.admin.api.controller.common.BaseController;
import com.taofuxn.admin.api.model.ResourceModel;
import com.taofuxn.admin.api.model.RoleModel;
import com.taofuxn.admin.api.model.SysUserModel;
import com.taofuxn.admin.api.model.response.SysUserPromiseResp;
import com.taofuxn.admin.api.service.SysUserConfigService;
import com.taofuxn.admin.common.exception.ExceptionEnum;
import com.taofuxn.admin.common.model.BaseResults;
import com.taofuxn.admin.common.util.CheckUtil;
import com.taofuxn.admin.dao.entity.TbSysResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.taofuxn.admin.common.exception.ExceptionEnum.SYSUSER_ROLE_NOT_FOUND_ERROR;

@Api("菜单资源管理")
@Slf4j
@RestController
@RequestMapping("/system/resource/")
public class ResourceController extends BaseController {

    @Autowired
    protected SysUserConfigService resourceService;

    @ApiOperation("树形菜单列表")
    @GetMapping("treelist")
    public BaseResults treelist() {
        List<ResourceModel> list = resourceService.getResourceList();
        return BaseResults.success(list);
    }


    @PostMapping("add")
    public BaseResults add(@RequestBody TbSysResource resource) {
        if (StringUtils.isBlank(resource.getName())) {
            return BaseResults.fail("资源名称不能为空");
        }
        resourceService.saveOrUpdateResource(resource);
        return BaseResults.success(null);
    }


    @PostMapping("edit")
    public BaseResults edit(@RequestBody TbSysResource resource) {
        if (resource.getId() == null) {
            return BaseResults.fail("资源ID不能为空");
        }
        if (StringUtils.isBlank(resource.getName())) {
            return BaseResults.fail("资源名称不能为空");
        }
        resourceService.saveOrUpdateResource(resource);
        return BaseResults.success(null);
    }


    @ApiOperation("删除菜单资源")
    @DeleteMapping("delete/{id}")
    public BaseResults delete(@PathVariable Long id) {
        if (id == null) {
            return BaseResults.fail("资源ID不能为空");
        }
        resourceService.deleteResource(id);
        return BaseResults.success(null);
    }


    @ApiOperation("获取用户菜单功能列表")
    @GetMapping("user")
    public BaseResults getUserResources(){
        SysUserModel sysUserModel = getCurrentUser();
        SysUserPromiseResp sysUserPromiseResp = resourceService.getUserDetailById(sysUserModel.getId());
        if (null == sysUserPromiseResp){
            return BaseResults.fail(ExceptionEnum.ACCOUNT_NOT_FOUND);
        }
        if (CollectionUtils.isEmpty(sysUserPromiseResp.getRoles())){
            return BaseResults.success(SYSUSER_ROLE_NOT_FOUND_ERROR);
        }
        List<Long> resourceIds = sysUserPromiseResp.getRoles().stream()
                        .filter(roleModel -> null != roleModel.getMenuIds())
                        .flatMap((Function<RoleModel, Stream<Long>>) roleModel -> roleModel.getMenuIds().stream())
                        .filter(CheckUtil::checkIdForLong)
                        .distinct()
                        .collect(Collectors.toList());
        List<ResourceModel> resourceModel = resourceService.getResourceById(resourceIds);
        if (CollectionUtils.isEmpty(resourceModel)){
            return BaseResults.fail(ExceptionEnum.RESOURCE_NOT_FOUND_ERROR);
        }
        return BaseResults.success(resourceModel);

    }

}
